#include "Thruster.h"

BasicThruster::BasicThruster() : PhysicsBox()
{

}

BasicThruster::BasicThruster(IDirect3DDevice9 *nDevice, D3DXVECTOR3 nPosition, D3DXVECTOR3 nVelocity, float nThrust)
	: PhysicsBox(nDevice, nPosition, nVelocity)
{
	IsActive = false;
	IsDestroyed = false;
	Thrust = nThrust;
	ThrustVector = D3DXVECTOR3(0,0,1);
	CreateCube(D3DXVECTOR3(.2f, .2f, .2f));
	InitCollisionBox(.2f, .2f, .2f);
	MainTrail = new ThrusterEffect(nDevice, "ThrusterEffect.fx", "Main", Position, 200, 0.01f);
	MainTrail->SetAngles(D3DXVECTOR3(0,0, 0));
	Angles = D3DXVECTOR3(0,0,0);
	Offset = D3DXVECTOR3(0,0,0);
	Throttle = 0;
}

BasicThruster::~BasicThruster()
{
	delete MainTrail;
}

void BasicThruster::Update( float dt )
{
	MainTrail->SetThrottle(Throttle);
	if(IsActive && !IsDestroyed)
	{
		D3DXMatrixRotationYawPitchRoll(&RotationMatrix, Angles.x, Angles.y, Angles.z);
		D3DXVECTOR3 RotatedVector;
		D3DXVec3TransformCoord(&RotatedVector, &ThrustVector, &RotationMatrix);

		Velocity += (RotatedVector) * (Thrust * dt);
		//MainTrail->Update(dt);
	}
	MainTrail->SetPosition(Position);
	MainTrail->SetAngles(Angles);
	PhysicsBox::Update(dt);
	MainTrail->Update(dt);
}

void BasicThruster::Render( void )
{
	MainTrail->Render();
	PhysicsBox::Render();
}

void BasicThruster::Activate( void )
{
	if(!IsDestroyed)
	{
		IsActive = true;
		MainTrail->Activate();
	}
}

void BasicThruster::Deactivate( void )
{
	IsActive = false;
	MainTrail->Deactivate();
}

void BasicThruster::Destroy( void )
{
	IsDestroyed = true;
	Deactivate();
}

bool BasicThruster::GetIsActive( void )
{
	return IsActive;
}

bool BasicThruster::GetIsDestroyed( void )
{
	return IsDestroyed;
}

void BasicThruster::SetAngles(D3DXVECTOR3 nAngles)
{
	Angles = nAngles;
	MainTrail->SetAngles(nAngles);
}

void BasicThruster::SetAngles(float nX, float nY, float nZ)
{
	Angles = D3DXVECTOR3(nX, nY, nZ);
	MainTrail->SetAngles(D3DXVECTOR3(nX, nY, nZ));
}

void BasicThruster::SetWVPMatrix(const D3DXMATRIX &Matrix)
{
	MainTrail->SetWVPMatrix(Matrix);
}

void BasicThruster::SetEyePosition(const D3DXVECTOR3 &nVector)
{
	MainTrail->SetCameraPosition(nVector);
}

void BasicThruster::SetOffset( D3DXVECTOR3 nOffset )
{
	Offset = nOffset;
}

D3DXVECTOR3 BasicThruster::GetOffset( void )
{
	return Offset;
}

D3DXVECTOR3 BasicThruster::GetAngles( void )
{
	return Angles;
}

void BasicThruster::SetThrottle( short nThrottle )
{
	Throttle = nThrottle;
}

void BasicThruster::SetSpawnVelocity(D3DXVECTOR3 nSpawnVelocity)
{
	MainTrail->SetSpawnVelocity(nSpawnVelocity);
}

bool BasicThruster::CreateCube( D3DXVECTOR3 nHalfVector)
{
	InitCollisionBox(nHalfVector);
	return BaseIndexedGraphicalEntity::CreateCube(nHalfVector);
}

void BasicThruster::SetPosition( D3DXVECTOR3 nPosition )
{
	MainTrail->SetPosition(nPosition);
	PhysicsBox::SetPosition(nPosition);
}